/**
 * Created with Intellij IDEA.
 * Description:
 * User:Lenovo
 * Date:2024-12-10
 * Time:20:02
 */
class SingleLinkedList{
    public ListNode head;

     class ListNode{
        private int val;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public void creatList(){
        ListNode node1 = new ListNode(10);
        ListNode node2 = new ListNode(20);
        ListNode node3 = new ListNode(30);
        ListNode node4 = new ListNode(40);
        ListNode node5 = new ListNode(50);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        head = node1;
    }
    public void display(){
         ListNode cur = head;
         while(cur!=null){
             if(cur == head){
                 System.out.print(cur.val+" ");
                 cur=cur.next;
             }else{
                 System.out.print(cur.val+" ");
                 cur=cur.next;
             }
         }
    }
    public void addlast(int val){
        SingleLinkedList.ListNode Node = new SingleLinkedList().new ListNode(val);
         ListNode cur = head;
         while (cur.next!=null){
             cur=cur.next;
         }
         cur.next=Node;
    }
    public void addfirst(int val){
        SingleLinkedList.ListNode Node = new SingleLinkedList().new ListNode(val);
         ListNode cur = head;
         head = Node;
         head.next=cur;
    }
    public void addindex(int index,int val){
        SingleLinkedList.ListNode Node = new SingleLinkedList().new ListNode(val);
         ListNode cur = head;
         int count = 0;
         while(count<index){
             cur=cur.next;
             count++;
         }
         ListNode cur2 = cur.next;
         cur.next = Node;
         Node.next = cur2;
    }
    public Boolean contains(int key){
//         SingleLinkedList.ListNode Node = new SingleLinkedList().new ListNode(key);
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }else{
                cur = cur.next;
            }
        }
        return false;
    }
}
public class Text {
    public static void main(String[] args) {
       SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.creatList();
        singleLinkedList.addlast(60);
        singleLinkedList.addfirst(9);
        singleLinkedList.addindex(6,70);
        System.out.println(singleLinkedList.contains(9));
        singleLinkedList.display();
    }
}
